
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class UiDependentHandle:

    def __init__(self):
        pass

    def get_element_all(self, driver, *args):
        return driver.find_element(*args)

    def get_elements_all(self, driver, *args):
        return driver.find_elements(*args)

    def click(self, element, *args):
        element.click()

    def js_special_handling(self, driver, element, operation):
        driver.execute_script(operation, element)

    def execute_script(self, driver, *operation):
        driver.execute_script(*operation)

    def slide_element_all(self, driver, *args):
        driver.swipe(*args)

    def get_size(self, driver):
        '''获取长宽'''
        x = driver.get_window_size()['width']
        y = driver.get_window_size()['height']
        return x, y

    def slide(self, driver, *args):
        x, y = self.get_size(driver)
        x1 = x * args[0]
        y1 = y * args[1]
        x2 = x * args[2]
        y2 = y * args[3]
        for i in range(args[5]):
            self.slide_element_all(driver, x1, y1, x2, y2, args[4])

    def input(self, element, *args):
        element.sed_keys(args[0])

    def empty(self, element, *args):
        element.clear()

    def into_frame(self, driver, *args):
        driver.switch_to.frame(args[0])

    def switch_back(self, driver):
        driver.switch_to.default_content()

    def into_alert(self, driver, *args):
        driver.switch_to.alert

    def the_current_window(self, driver):
        return driver.current_window_handle

    def into_window(self, driver, *args):
        driver.switch_to.window(driver.window_handles[args[0]])

    def into_context(self, driver, *args):
        driver.switch_to.context(args[0])

    def display_wait(self, driver, *args):
        return WebDriverWait(driver, args[2][0], args[2][1]).until(
            EC.presence_of_element_located(args[:2])
        )

    def displays_wait(self, driver, *args):
        return WebDriverWait(driver, args[2][0], args[2][1]).until(lambda x: x.find_elements(*args[:2]))

    def implicit_wait(self, driver, *args):
        driver.implicitly_wait(args[0])

    def forced_wait(self, driver, *args):
        time.sleep(args[0])

    def web_drag(self, driver, *args):
        tab = ActionChains(driver)
        # 获取滑动按钮
        element = driver.find_element(*args[0])
        # 使用随机数确定滑动位置后滑动
        tab.drag_and_drop_by_offset(element, args[1], args[2]).perform()
        # 滑动结束后释放鼠标
        tab.move_to_element(element).release()

    def app_drag(self, driver, *args):   # args = ("", ((),()))
        save_button = driver.find_element(*args[0])
        more_button = driver.find_element(*args[1])
        driver.drag_and_drop(save_button, more_button)

    def screenshot(self, driver, *path):
        driver.save_screenshot(*path)


































































